package com.kotlin.book.fragments

import android.graphics.Color
import android.graphics.Typeface
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.PopupWindow
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import com.kotlin.book.R
import com.kotlin.book.adapter.ShelfFragmentAdapter
import com.kotlin.book.databinding.FragmentShelfBinding
import com.kotlin.book.databinding.PopupShelfShowBinding
import com.kotlin.book.util.Util

class ShelfFragment : Fragment() {
    private lateinit var binding: FragmentShelfBinding


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragmentShelfBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        //为顶部导航栏绑定事件
        for (i in 0 until binding.navbarBox.childCount) {
            binding.navbarBox.getChildAt(i).setOnClickListener {
                setTextViewStyle(i)
                binding.shelfContentViewPager.currentItem = i
            }
        }
        //首页选中
        setTextViewStyle(0)
        //设置书架
        binding.shelfContentViewPager.adapter = ShelfFragmentAdapter(this)
        //监听翻页
        binding.shelfContentViewPager.registerOnPageChangeCallback(object :
            ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                setTextViewStyle(position)
            }
        })
        //设置icon点击事件
        binding.iconList.setOnClickListener {
            val location = IntArray(2)
            it.getLocationInWindow(location)
            showPopupWindow(location[0], location[1])
        }
    }

    //设置头部文字选中样式
    private fun setTextViewStyle(position: Int) {
        for (i in 0 until binding.navbarBox.childCount) {
            val textView = binding.navbarBox.getChildAt(i) as TextView
            if (i == position) {
                textView.background =
                    ContextCompat.getDrawable(
                        requireContext(),
                        R.drawable.shelf_navbar_bottom
                    )
                textView.setTextColor(
                    ContextCompat.getColor(
                        requireContext(),
                        R.color.primary_color
                    )
                )
                textView.setTypeface(null, Typeface.BOLD)
            } else {
                textView.background = null
                textView.setTextColor(
                    ContextCompat.getColor(
                        requireContext(),
                        R.color.text_color_primary
                    )
                )
                textView.setTypeface(null, Typeface.NORMAL)
            }
        }
    }

    private fun showPopupWindow(x: Int, y: Int) {
        // 获取弹出窗口的视图
        val popupBinding = PopupShelfShowBinding.inflate(layoutInflater)
        val popupView = popupBinding.root
        popupView.setPadding(x - Util.dpToPx(45, requireContext()), y, 0, 0)

        // 创建一个全屏蒙版视图
        val maskView = View(requireContext())  // 使用 requireContext() 获取 Context
        maskView.setBackgroundColor(Color.parseColor("#80000000"))  // 设置半透明的黑色蒙版
        maskView.layoutParams = ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT
        )

        // 使用 FrameLayout 来容纳蒙版视图和 PopupWindow 内容
        val frameLayout = FrameLayout(requireContext())
        frameLayout.addView(maskView)  // 先添加蒙版视图
        frameLayout.addView(popupView) // 然后添加弹窗内容

        // 创建 PopupWindow
        val popupWindow = PopupWindow(
            frameLayout,
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT,
            true
        )

        // 显示 PopupWindow
        popupWindow.showAtLocation(binding.root, Gravity.NO_GRAVITY, 0, 0)

        popupBinding.showMode.setOnClickListener {
            val fragment =
                childFragmentManager.findFragmentByTag("f${binding.shelfContentViewPager.currentItem}") as? ShelfFragmentIndex
            fragment?.updateShowMode()
            popupWindow.dismiss()
        }

        // 如果你想点击蒙版关闭 PopupWindow
        maskView.setOnClickListener {
            popupWindow.dismiss()
        }
    }
}